﻿function Init()
{
   if (typeof(SetupIme) != "undefined")
		SetupIme();

	//键盘处理“上一页”，“下一页”
	$("body").keyup(function(e)
	{
		if (e.target.tagName == "INPUT" || e.target.tagName == "TEXTAREA")
			return;

		if (e.keyCode == 37)//上一页
		{
			if ($("#PrevPage").length > 0)
				window.location = $("#PrevPage").attr("href");
		}
		else if (e.keyCode == 39)//下一页
		{
			if ($("#NextPage").length > 0)
				window.location = $("#NextPage").attr("href");
		}
	});
	
	//ToolTip
	if (typeof(_lang) != "undefined" && (_lang == "UYG" || _lang == "KZA" || _lang == "KGA"))
		SetTip();
	
	$(".List1").each(function(){$(this).children(":odd").addClass("Even");});
	
	if (typeof(_page) == "undefined")
		return;
	
	if (_page == "index")
	{
		$(".TabMenu li").click(function()
		{
			var me = $(this);
			if (me.hasClass("Selected"))
				return false;
				
			var old = me.siblings(".Selected");
			old.removeClass("Selected");
			old.find("a").attr("href", "");
			
			me.addClass("Selected");
			me.find("a").removeAttr("href");
			
			var tabmenu = me.parent().eq(0);
			var list = tabmenu.next();
			if (old.attr("id") == "")
				old.data("visited", list.html());

			if (me.attr("id") == "")
				list.html(me.data("visited"));
			else
				list.load(_listPath + "?PageSize=16&CategoryID=" + me.attr("id").substr(1), null, function(){$(this).children(":odd").addClass("Even");});
			
			return false;
		});
		
		$(".Title1 .Left").each(function()
		{
			var left = $(this);
			left.find("a:last").click(function()
			{
				var title = left.parent().eq(0);
				var list = title.next();
				
				var sort = "";
				var right = left.next();
				var index = right.children().index(right.children(".Selected"));
				switch (index)
				{
					case 1:
						sort = "&Sort=Rank";
						break;
					case 2:
						sort = "&Sort=Recommend";
						break;
					case 3:
						sort = "&Sort=Up";
						break;
				}
				
				var logo = (title.data("logo") != "true" || typeof(title.data("logo")) == "undefined") ? "true" : "false";
				
				list.load(_listPath + "?PageSize=50&CategoryID=" + title.attr("id").substr(1) + "&Logo=" + logo + sort, null, function(){$(this).children(":odd").addClass("Even");});
				
				if (logo == "true")
				{
					title.data("logo", "true");
					$(this).text(_lang_by_text);
				}
				else
				{
					title.data("logo", "false");
					$(this).text(_lang_by_logo);
				}

				return false;
			});
		});
		
		$(".Title1 .Right").each(function()
		{
			var right = $(this);
			right.children().click(function()
			{
				var me = $(this);
				if (me.hasClass("Selected"))
					return false;
					
				var old = me.siblings(".Selected");
				old.removeClass("Selected");
				old.find("a").attr("href", "");
				
				me.addClass("Selected");
				me.find("a").removeAttr("href");
				
				var title = right.parent().eq(0);
				var list = title.next();
				
				var sort = "";
				var index = right.children().index(me);
				switch (index)
				{
					case 1:
						sort = "&Sort=Rank";
						break;
					case 2:
						sort = "&Sort=Recommend";
						break;
					case 3:
						sort = "&Sort=Up";
						break;
				}
				
				list.load(_listPath + "?PageSize=50&CategoryID=" + title.attr("id").substr(1) + "&Logo=" + title.data("logo") + sort, null, function(){$(this).children(":odd").addClass("Even");});
					
				return false;
			});
		});
		
		//-----------------------------------------
		//Start : 最近访问
		var html = "";
		var siteIDs = GetCookie("SiteIDs").split("|");
		var siteNames = GetCookie("SiteNames").split("|");
		var siteLogos = GetCookie("SiteLogos").split("|");
		for (var i = 0; i < siteIDs.length; i++)
		{
			if (siteIDs[i] == "")
				continue;
				
			if (i % 4 == 0)
				html += "<ul><!--li--></ul>";
			if (siteLogos[i] == null || siteLogos[i] == "" || siteLogos[i] == "undefined")
				html = html.replace("<!--li-->", "<li><a href='" + _linkPath.replace("{ID}", siteIDs[i]) + "' target='_blank'>" + siteNames[i] + "</a></li><!--li-->");
			else
				html = html.replace("<!--li-->", "<li><a href='" + _linkPath.replace("{ID}", siteIDs[i]) + "' target='_blank'><img src='" + siteLogos[i] + "' /></a></li><!--li-->");
		}
		$(".List1:first").html(html);
		//End : 最近访问
		//-----------------------------------------

		//-----------------------------------------
		//Start : 添加入站
		var referrer = document.referrer;
		if (referrer != null && referrer != "" && referrer.indexOf("http://" + document.domain) != 0)
			$.post(_webPath + "alicms.aspx?Object=Link&Operation=IncrementIn&Referrer=" + encodeURIComponent(referrer), {});
		//End : 添加入站
		//-----------------------------------------
	}
	else if (_page == "link")
	{
		$.post(_webPath + "alicms.aspx?Object=Link&Operation=IncrementOut&ID=" + _id, {});
		
		window.moveTo(0, 0);
		window.resizeTo(window.screen.width, window.screen.height);
		$("iframe")[0].height = $("html").height() - $(".Block1").height();
		window.onresize = function(){$("iframe")[0].height = $("html").height() - $(".Block1").height();};
		
		$("#Close").click(function()
		{
		   window.location.href = $("iframe")[0].src;
		});

		$("#Up").click(function()
		{
		   if ($(this).hasClass("Disabled"))
				return false;
				
			$.post(_webPath + "alicms.aspx?Object=Link&Operation=IncrementUp&ID=" + _id, {});
			$(this).addClass("Disabled");
			return false;
		});

		$("#Down").click(function()
		{
		   if ($(this).hasClass("Disabled"))
				return false;
				
			$.post(_webPath + "alicms.aspx?Object=Link&Operation=IncrementDown&ID=" + _id, {});
			$(this).addClass("Disabled");
			return false;
		});

		$("#Error").click(function()
		{
		   if ($(this).hasClass("Disabled"))
				return false;
				
			$.post(_webPath + "alicms.aspx?Object=Link&Operation=IncrementError&ID=" + _id, {});
			$(this).addClass("Disabled");
			return false;
		});
		
		var index = -1;
		var siteIDs = GetCookie("SiteIDs");
		var array = siteIDs.split("|");
		siteIDs = _pageID;
		var length = Math.min(array.length, 23);
		for (var i = 0; i < length; i++)
		{
			if (array[i] == "")
				length--;
			else if (array[i] == _pageID)
				index = i;
			else
				siteIDs += "|" + array[i];
		}
		SetCookie("SiteIDs", siteIDs, 360*24*60, "/");
		
		var siteNames = GetCookie("SiteNames");
		var array = siteNames.split("|");
		siteNames = _name;
		for (var i = 0; i < length; i++)
		{
			if (i != index)
				siteNames += "|" + array[i];
		}
		SetCookie("SiteNames", siteNames, 360*24*60, "/");
		
		var siteLogos = GetCookie("SiteLogos");
		var array = siteLogos.split("|");
		siteLogos = _logo;
		for (var i = 0; i < length; i++)
		{
			if (i != index)
				siteLogos += "|" + ((array[i] == null || array[i] == "" || array[i] == "undefined") ? "" : array[i]);
		}
		SetCookie("SiteLogos", siteLogos, 360*24*60, "/");
		
		//获取人气，下载次数，顶，踩
		$("#Out").load(_webPath + "alicms.aspx?Object=Link&Operation=GetOut&ID=" + _id);
		$("#Error span").load(_webPath + "alicms.aspx?Object=Link&Operation=GetError&ID=" + _id);
		$("#Up span").load(_webPath + "alicms.aspx?Object=Link&Operation=GetUp&ID=" + _id);
		$("#Down span").load(_webPath + "alicms.aspx?Object=Link&Operation=GetDown&ID=" + _id);
	}
	else if (_page == "tag")
	{
		$.post(_webPath + "alicms.aspx?Object=Tag&Operation=IncrementHitCount&ID=" + _id, {});
	}
}

Init();